package cn.tedu._07springaoptest.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class logAspect {
    //    @Before(value = "execution(public boolean cn.tedu._07springaoptest.aop.CartServiceImpl.*(..))")
//    public void beforeMethod(JoinPoint joinPoint){
//        String name = joinPoint.getSignature().getName();
//        String args = Arrays.toString(joinPoint.getArgs());
//        System.out.println("[前置通知:]方法"+name+"加入购物车内容为:"+args);
//    }
//    @AfterReturning(value = "execution(public boolean cn.tedu._07springaoptest.aop.CartServiceImpl.*(..))",returning = "r")
//    public void afterReturningMethod(JoinPoint joinPoint,Object r){
//        String name = joinPoint.getSignature().getName();
//        System.out.println("[返回通知:]方法"+name+"返回结果"+r);
//    }
//    @AfterThrowing(value = "execution(public boolean cn.tedu._07springaoptest.aop.CartServiceImpl.*(..))",throwing = "ex")
//    public void afterThrowing(JoinPoint joinPoint,Throwable ex){
//        String name = joinPoint.getSignature().getName();
//        String message = ex.getMessage();
//        System.out.println("[异常通知]:"+name+"方法异常："+message);
//    }
//    @After(value = "execution(public int cn.tedu._07springaoptest.aop.CartServiceImpl.*(..))")
//    public void afterMethod(JoinPoint joinPoint){
//        String name = joinPoint.getSignature().getName();
//        System.out.println("[后置通知:]"+name+"方法彻底结束");
//    }
    @Around(value = "execution(public boolean cn.tedu._07springaoptest.aop.CartServiceImpl.*(..))")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) {
        boolean result = false;
        try {
            //前置通知
            System.out.println("1.前置通知");
            if (result) {
                result = (boolean) joinPoint.proceed();
            }

            //返回通知
            System.out.println("2.返回通知");
        } catch (Throwable e) {
            System.out.println("3.异常通知");
            throw new RuntimeException(e);
        } finally {
            System.out.println("4.后置通知");
        }
        return result;
    }
}
